1. MongoDB Find Operations
Definition
The find() method in MongoDB is used to query documents from a collection. It allows you to retrieve documents that match specific criteria and supports various query operators for complex searches. The find operation is fundamental for data retrieval in MongoDB.
Algorithm 1: Basic Find Query :-
Example 1: Simple Find Query
// Find all students with age greater than 20
db.students.find({
age: { $gt: 20 }
})
Algorithm 2: Find with Multiple Conditions :-
Example 2: Complex Find Query
// Find documents with multiple conditions
db.products.find({
$and: [
{ price: { $gte: 100 } },
{ category: "electronics" },
{ inStock: true }
]
})
Algorithm 3: Find with Projection :-
Example 3: Find with Projection
// Find with specific field projection
db.users.find(
{ age: { $gt: 25 } },
{
name: 1,
email: 1,
_id: 0
}
).sort({ name: 1 })